package com.coco.mapper;

import com.coco.pojo.LeaveRequest;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 请假申请Mapper接口
 */
@Mapper
public interface LeaveRequestMapper {

    /**
     * 插入新的请假申请记录
     * @param leaveRequest 请假申请对象
     * @return 受影响的行数
     */
    @Insert("INSERT INTO leave_request(user_id, leave_type, start_date, end_date, reason, status, approver_id, approve_time) " +
            "VALUES(#{userId}, #{leaveType}, #{startDate}, #{endDate}, #{reason}, #{status}, #{approverId}, #{approveTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(LeaveRequest leaveRequest);

    /**
     * 根据ID更新请假申请信息
     * @param leaveRequest 请假申请对象
     * @return 受影响的行数
     */
    @Update("UPDATE leave_request SET user_id=#{userId}, leave_type=#{leaveType}, start_date=#{startDate}, " +
            "end_date=#{endDate}, reason=#{reason}, status=#{status}, approver_id=#{approverId}, approve_time=#{approveTime} " +
            "WHERE id=#{id}")
    int updateById(LeaveRequest leaveRequest);

    /**
     * 根据ID删除请假申请记录
     * @param id 请假申请ID
     * @return 受影响的行数
     */
    @Delete("DELETE FROM leave_request WHERE id=#{id}")
    int deleteById(Integer id);

    /**
     * 根据ID查询请假申请信息
     * @param id 请假申请ID
     * @return 请假申请对象
     */
    @Select("SELECT * FROM leave_request WHERE id=#{id}")
    @Results(id = "leaveRequestMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "leave_type", property = "leaveType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "start_date", property = "startDate", jdbcType = JdbcType.DATE),
            @Result(column = "end_date", property = "endDate", jdbcType = JdbcType.DATE),
            @Result(column = "reason", property = "reason", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.VARCHAR),
            @Result(column = "approver_id", property = "approverId", jdbcType = JdbcType.INTEGER),
            @Result(column = "approve_time", property = "approveTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LeaveRequest selectById(Integer id);

    /**
     * 查询所有请假申请信息
     * @return 请假申请对象列表
     */
    @Select("SELECT * FROM leave_request")
    @ResultMap("leaveRequestMap")
    List<LeaveRequest> selectAll();

    /**
     * 根据条件查询请假申请信息
     * @param leaveRequest 查询条件
     * @return 请假申请对象列表
     */
    @Select("<script>" +
            "SELECT * FROM leave_request" +
            "<where>" +
            "    <if test='userId != null'>" +
            "        AND user_id = #{userId}" +
            "    </if>" +
            "    <if test='leaveType != null and leaveType != \"\"'>" +
            "        AND leave_type = #{leaveType}" +
            "    </if>" +
            "    <if test='status != null and status != \"\"'>" +
            "        AND status = #{status}" +
            "    </if>" +
            "</where>" +
            "</script>")
    @ResultMap("leaveRequestMap")
    List<LeaveRequest> selectByEntity(LeaveRequest leaveRequest);

    /**
     * 更新请假申请状态
     * @param id 请假申请ID
     * @param status 新状态
     * @return 受影响的行数
     */
    @Update("UPDATE leave_request SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") String status);

    /**
     * 分页查询请假申请信息
     * @param userId 用户ID
     * @param leaveType 请假类型
     * @param status 请假状态
     * @param pageSize 每页大小
     * @return 请假申请对象列表
     */
    @Select("<script>" +
            "SELECT * FROM leave_request" +
            "<where>" +
            "    <if test='userId != null'>" +
            "        AND user_id = #{userId}" +
            "    </if>" +
            "    <if test='leaveType != null and leaveType != \"\"'>" +
            "        AND leave_type = #{leaveType}" +
            "    </if>" +
            "    <if test='status != null and status != \"\"'>" +
            "        AND status = #{status}" +
            "    </if>" +
            "</where>" +
            "</script>")
    @ResultMap("leaveRequestMap")
    List<LeaveRequest> selectPage(@Param("userId") Integer userId, @Param("leaveType") String leaveType, @Param("status") String status, @Param("offset") int offset, @Param("pageSize") int pageSize);

    /**
     * 查询请假申请总数
     * @param userId 用户ID
     * @param leaveType 请假类型
     * @param status 请假状态
     * @return 请假申请总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM leave_request" +
            "<where>" +
            "    <if test='userId != null'>" +
            "        AND user_id = #{userId}" +
            "    </if>" +
            "    <if test='leaveType != null and leaveType != \"\"'>" +
            "        AND leave_type = #{leaveType}" +
            "    </if>" +
            "    <if test='status != null and status != \"\"'>" +
            "        AND status = #{status}" +
            "    </if>" +
            "</where>" +
            "</script>")
    int count(@Param("userId") Integer userId, @Param("leaveType") String leaveType, @Param("status") String status);
}